package com.hck.weight_test.ui;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.hck.weight_test.ObtainWeighing.WeightLogUtil;
import com.hck.weight_test.ObtainWeighing.bean.DeviceBean;
import com.hck.weight_test.ObtainWeighing.device.DeviceManger;
import com.hck.weight_test.ObtainWeighing.interfaces.IDeviceGetDataListener;
import com.hck.weight_test.R;

public class MainActivity extends Activity {
    TextView textView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.weightTv);
        startGetWeight();
    }
    private void startGetWeight(){
        WeightLogUtil.log("startGetWeight");
        //Device{deviceId='/dev/ttyS3', deviceType='serial_port', deviceName='ttyS3 (serial)', manufacturerName='null', serialPortParameter=null}
        DeviceBean deviceBean=new DeviceBean();
        deviceBean.setDeviceName("成虫");
        deviceBean.setDeviceId("/dev/ttyS3");
        deviceBean.setBaudrate(9600);
        deviceBean.setFlags(0);
        DeviceManger.getDeviceManger().startGetWeight(deviceBean, new IDeviceGetDataListener() {
            @Override
            public void onGetData(DeviceBean device, String data) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                      //  Log.d("hck","dddd222: "+data);
                        if (TextUtils.isEmpty(data)){
                            return;
                        }
                        String datas[]=data.split(" ");
                        if (datas!=null && datas.length>0){
                            for (String d:datas){
                               if (!TextUtils.isEmpty(d)){
                                   double dataD=Double.parseDouble(d);
                                   textView.setText((dataD/1000*2)+"斤");
                               }
                            }
                        }

                    }
                });


            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DeviceManger.getDeviceManger().close();
    }
}
